package com.spring.shops.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.spring.shops.dao.TzIndexImgMapper;
import com.spring.shops.dao.TzProdMapper;
import com.spring.shops.model.entity.TzIndexImg;
import com.spring.shops.model.result.ServerResponseEntityVoid;
import com.spring.shops.model.dto.IndexImgDTO;
import com.spring.shops.model.result.IPageIndexImg;
import com.spring.shops.model.vo.IndexImg;
import com.spring.shops.service.IndexImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IndexImageServiceImpl implements IndexImageService {
    @Autowired
    private TzIndexImgMapper mapper;
    @Autowired
    private TzProdMapper prodMapper;
    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public ServerResponseEntityVoid<IPageIndexImg<IndexImg>> page(IndexImgDTO dto) {
        //获取当前页码
        Long currentPage = dto.getCurrent();
        //获取每页显示的条数
        Long size= dto.getSize();
        //设置分页初始值
        PageHelper.startPage(Math.toIntExact(currentPage), Math.toIntExact(size));
        //调用mapper层进行分页查询
        Page<TzIndexImg> indexImgPage=mapper.page(dto);
        //创建IPageIndexImg对象
        IPageIndexImg<IndexImg> iPageIndexImg=new IPageIndexImg<>();
        iPageIndexImg.setSize(size);
        iPageIndexImg.setTotal(indexImgPage.getTotal());
        iPageIndexImg.setCurrent(currentPage);
        iPageIndexImg.setPages( indexImgPage.getTotal()/size+1);
        //获取所有记录
        List<TzIndexImg> result = indexImgPage.getResult();
        List<IndexImg> data = new ArrayList<>();
        //属性拷贝
        for (TzIndexImg tzIndexImg : result) {
            IndexImg indexImg1 = getIndexImg(tzIndexImg);
            //加入集合中
            data.add(indexImg1);
        }
        iPageIndexImg.setRecords(data);
        //返回结果集
        return ServerResponseEntityVoid.success(iPageIndexImg);
    }
    //创建一个方法用来将实体转换为IndexImg实体
    public IndexImg getIndexImg(TzIndexImg tzIndexImg){
        int imgId = tzIndexImg.getImgId().intValue();
        int shopId = tzIndexImg.getShopId().intValue();
        int relation=tzIndexImg.getRelation().intValue();
        Boolean status = tzIndexImg.getStatus();
        Integer status2=0;
        if(status){
            status2=1;
        }
        //获取该图片相关联的产品id
        Long prodId = tzIndexImg.getRelation();
        // 创建一个SimpleDateFormat对象，定义日期和时间的格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 使用format方法将Date对象转换为String对象
        String uploadTime = dateFormat.format(tzIndexImg.getUploadTime());
        //创建IndexImg进行赋值
        IndexImg indexImg1 = new IndexImg(imgId,shopId,tzIndexImg.getImgUrl(), tzIndexImg.getDes(),
                tzIndexImg.getTitle(), tzIndexImg.getLink(), status2,tzIndexImg.getSeq(),
                uploadTime,tzIndexImg.getType(),relation,null,null);
        return indexImg1;
    }
    //创建一个方法用来将IndexImg实体转换为实体
    public TzIndexImg getTzIndexImg(IndexImg indexImg) throws ParseException {
        //shopId设置默认值
        Long shopId=1L;
        Boolean status= indexImg.getStatus() == 1;
        //获取类型
        Integer type = indexImg.getType();
        Long relation;
        if(type==-1){
            //单独的图
            relation=-1L;
        }else{
            relation= Long.valueOf(indexImg.getRelation());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return new TzIndexImg(shopId,indexImg.getImgUrl(),
                indexImg.getDes(),"","",
                status, indexImg.getSeq(),new Date(),relation, indexImg.getType());
    }

    /**
     * 根据id查询信息
     *
     * @param imgId
     * @return
     */
    @Override
    public IndexImg getById(Integer imgId) {
        Long id= Long.valueOf(imgId);
        TzIndexImg tzIndexImg = mapper.selectByPrimaryKey(id);
       return getIndexImg(tzIndexImg);
    }
    /**
     * 批量删除数据
     * @param integers
     * @return
     */
    @Override
    public boolean deleteByIds(List<Integer> integers) {
        int rowAffected= mapper.deleteByIds(integers);
        if(rowAffected>0){
            return true;
        }else return false;
    }
    /**
     * 新增数据
     * @param indexImg
     * @return
     */
    @Override
    public boolean save(IndexImg indexImg) {
        try {
            TzIndexImg tzIndexImg = getTzIndexImg(indexImg);
            int rows = mapper.insert(tzIndexImg);
            if(rows>0){
                return true;
            }else return false;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 更新数据
     * @param indexImg
     * @return
     */
    @Override
    public boolean update(IndexImg indexImg){
        try {
            TzIndexImg tzIndexImg = getTzIndexImg(indexImg);
            int rows = mapper.updateByPrimaryKey(tzIndexImg);
            if(rows>0){
                return true;
            }else return false;

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
